/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.entity.PhongTro;
import model.service.*;
import model.dao.*;
import model.service.UsersDAOService;

/**
 *
 * @author duy
 */
public class PhongTroDAO implements PhongTroDAOService {

    private static PhongTroDAO phongtroDAO;

    QuanDAOService quanDAOService;
    UsersDAOService usersDAOService;

    public static PhongTroDAO getInstance() {
        if (phongtroDAO == null) {
            phongtroDAO = new PhongTroDAO();

        }
        return phongtroDAO;
    }

    public PhongTroDAO() {
        quanDAOService = QuanDAO.getInstance();
        usersDAOService = UserDAO.getInstance();
    }

    // Lấy tất cả Phòng trọ trong ban phòng trọ;
    @Override
    public List<PhongTro> getAllPhongTro() {
        List<PhongTro> list = new ArrayList<>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tbl_phongtro";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                PhongTro item = new PhongTro();
                item.setMaphongtro(rs.getInt("maphongtro"));
                item.setTieudegioithieu(rs.getString("tieudegioithieu"));
                item.setTenchunha(rs.getString("tenchunha"));
                item.setSdt(rs.getInt("sdt"));
                item.setDiachi(rs.getString("diachi"));
                item.setDanhgia(rs.getInt("danhgia"));
                item.setSophong(rs.getInt("sophong"));
                item.setDientich(rs.getInt("dientich"));
                item.setGhichu(rs.getString("ghichu"));
                item.setNgaydang(rs.getDate("ngaydang"));
                item.setGiaphong(rs.getInt("giaphong"));
                item.setKinhdo(rs.getFloat("kinhdo"));
                item.setVido(rs.getFloat("vido"));
                item.setSonguoidanhgia(rs.getInt("songuoidanhgia"));
                item.setDiachiMAC(rs.getString("diachiMAC"));
                item.setIsActive(rs.getBoolean("isActive"));;

                int maquan = rs.getInt("maquan");
                item.setQuan(quanDAOService.getQuanbyMa(maquan));

                maquan = rs.getInt("mauser");
                item.setUser(usersDAOService.getusersbyID(maquan));
                list.add(item);
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;

    }

    @Override
    public List<PhongTro> getPhongTrobyQUan(int maquan) {
        List<PhongTro> list = new ArrayList<>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tbl_phongtro where maquan=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, maquan);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                PhongTro item = new PhongTro();
                item.setMaphongtro(rs.getInt("maphongtro"));
                item.setTieudegioithieu(rs.getString("tieudegioithieu"));
                item.setTenchunha(rs.getString("tenchunha"));
                item.setSdt(rs.getInt("sdt"));
                item.setDiachi(rs.getString("diachi"));
                item.setDanhgia(rs.getInt("danhgia"));
                item.setSophong(rs.getInt("sophong"));
                item.setDientich(rs.getInt("dientich"));
                item.setGhichu(rs.getString("ghichu"));
                item.setNgaydang(rs.getDate("ngaydang"));
                item.setKinhdo(rs.getFloat("kinhdo"));
                item.setVido(rs.getFloat("vido"));
                item.setSonguoidanhgia(rs.getInt("songuoidanhgia"));
                item.setDiachiMAC(rs.getString("diachiMAC"));
                item.setIsActive(rs.getBoolean("isActive"));;

                int mauser = rs.getInt("mauser");
                item.setUser(usersDAOService.getusersbyID(mauser));

                list.add(item);
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public PhongTro getThongtinPTbyMaPhongTro(int maphongtro) {
        PhongTro item = new PhongTro();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tbl_phongtro where maphongtro=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, maphongtro);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                item.setMaphongtro(rs.getInt("maphongtro"));
                item.setTieudegioithieu(rs.getString("tieudegioithieu"));
                item.setTenchunha(rs.getString("tenchunha"));
                item.setSdt(rs.getInt("sdt"));
                item.setDiachi(rs.getString("diachi"));
                item.setDanhgia(rs.getInt("danhgia"));
                item.setSophong(rs.getInt("sophong"));
                item.setDientich(rs.getInt("dientich"));
                item.setGhichu(rs.getString("ghichu"));
                item.setNgaydang(rs.getDate("ngaydang"));
                item.setKinhdo(rs.getFloat("kinhdo"));
                item.setVido(rs.getFloat("vido"));
                item.setSonguoidanhgia(rs.getInt("songuoidanhgia"));
                item.setDiachiMAC(rs.getString("diachiMAC"));
                item.setIsActive(rs.getBoolean("isActive"));;

                int maquan = rs.getInt("maquan");
                item.setQuan(quanDAOService.getQuanbyMa(maquan));

                maquan = rs.getInt("mauser");
                item.setUser(usersDAOService.getusersbyID(maquan));
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return item;
    }

    @Override
    public boolean createPhongTro(PhongTro phongTro) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();//thieu dau phay cho nay
            String sql = "insert into tbl_phongtro(maphongtro,tieudegioithieu, tenchunha, sdt, diachi, danhgia,"
                    + " sophong, dientich,ghichu,ngaydang,giaphong,kinhdo,vido,isActive,mauser,maquan) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);

            pstmt.setInt(1, phongTro.getMaphongtro());
            pstmt.setString(2, phongTro.getTieudegioithieu());
            pstmt.setString(3, phongTro.getTenchunha());
            pstmt.setInt(4, phongTro.getSdt());
            pstmt.setString(5, phongTro.getDiachi());
            pstmt.setFloat(6, phongTro.getDanhgia());
            pstmt.setInt(7, phongTro.getSophong());
            pstmt.setInt(8, phongTro.getDientich());
            pstmt.setString(9, phongTro.getGhichu());
            pstmt.setDate(10, (Date) phongTro.getNgaydang());
            pstmt.setInt(11, phongTro.getGiaphong());
            pstmt.setFloat(12, phongTro.getKinhdo());
            pstmt.setFloat(13, phongTro.getVido());
            pstmt.setBoolean(14, phongTro.isIsActive());
            pstmt.setInt(15, phongTro.getUser().getMauser());
            pstmt.setInt(16, phongTro.getQuan().getMaquan());

            pstmt.executeUpdate();
            isCheck = true;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return isCheck;
    }

    @Override
    public boolean xoa(int maphongtro) throws Exception {
        String sql = "delete from tbl_phongtro where maphongtro =?";
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement sm = conn.prepareStatement(sql);
        sm.setInt(1, maphongtro);
        return sm.executeUpdate() == 1;
    }

    @Override
    public boolean editPhongTro(PhongTro phongTro) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();//thieu dau phay cho nay
            String sql = "update tbl_phongtro set tieudegioithieu=?,tenchunha=?,sdt=?,diachi=?,danhgia=?,sophong=?,dientich=?,ghichu=?,"
                    + "ngaydang=?,giaphong=?,kinhdo=?,vido=?,isActive=?,mauser=?,maquan=? where maphongtro = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);

            pstmt.setInt(16, phongTro.getMaphongtro());
            pstmt.setString(1, phongTro.getTieudegioithieu());
            pstmt.setString(2, phongTro.getTenchunha());
            pstmt.setInt(3, phongTro.getSdt());
            pstmt.setString(4, phongTro.getDiachi());
            pstmt.setFloat(5, phongTro.getDanhgia());
            pstmt.setInt(6, phongTro.getSophong());
            pstmt.setInt(7, phongTro.getDientich());
            pstmt.setString(8, phongTro.getGhichu());
            pstmt.setDate(9, (Date) phongTro.getNgaydang());
            pstmt.setInt(10, phongTro.getGiaphong());
            pstmt.setFloat(11, phongTro.getKinhdo());
            pstmt.setFloat(12, phongTro.getVido());
            pstmt.setBoolean(13, phongTro.isIsActive());
            pstmt.setInt(14, phongTro.getUser().getMauser());
            pstmt.setInt(15, phongTro.getQuan().getMaquan());

            pstmt.executeUpdate();
            isCheck = true;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return isCheck;
    }

    @Override
    public boolean update_danhgia(int maphongtro, float danhgia,int songuoidanhgia, String diachiMAC) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();//thieu dau phay cho nay
            String sql = "update tbl_phongtro set danhgia=?,songuoidanhgia=?,diachiMAC=? where maphongtro = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
             pstmt.setFloat(1,danhgia);
             pstmt.setInt(2,songuoidanhgia);
             pstmt.setString(3, diachiMAC);
             pstmt.setInt(4, maphongtro);
            pstmt.executeUpdate();
            isCheck = true;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return isCheck;
    }


}
